/**
 * @description
 */


package com.yjx.crm.controller;

import com.yjx.crm.base.BaseController;
import com.yjx.crm.base.ResultInfo;
import com.yjx.crm.exceptions.ParamsException;
import com.yjx.crm.model.UserModel;
import com.yjx.crm.query.UserQuery;
import com.yjx.crm.service.UserService;
import com.yjx.crm.utils.LoginUserUtil;
import com.yjx.crm.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController extends BaseController {
    @Resource
    private UserService userService;


    /*用户登录*/
    @PostMapping("login")
    @ResponseBody
    public ResultInfo userLogin(String userName,String userPwd) {
        ResultInfo resultInfo = new ResultInfo();
        //调用Service层的方法
        UserModel userModel =  userService.userlogin(userName,userPwd);

        //设置ResultInfo的result的值（将数据返回给请求）
        resultInfo.setResult(userModel);
//        try {
//            //调用Service层的方法
//           UserModel userModel =  userService.userlogin(userName,userPwd);
//
//           //设置ResultInfo的result的值（将数据返回给请求）
//            resultInfo.setResult(userModel);
//
//        } catch (ParamsException p) {
//            resultInfo.setCode(p.getCode());
//            resultInfo.setMsg(p.getMsg());
//            p.printStackTrace();
//        } catch (Exception e) {
//            resultInfo.setCode(500);
//            resultInfo.setMsg("登录失败!");
//        }
        return resultInfo;
    }

    @PostMapping("updatePwd")
    @ResponseBody
    public ResultInfo updateUserPassword(HttpServletRequest request,
                                         String oldPassword,String newPassword,String repeatPassword) {
        ResultInfo resultInfo = new ResultInfo();

        //获取cookie中的userId
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
        //调用Service层修改密码方法
        userService.updatePassword(userId,oldPassword,newPassword,repeatPassword);


//        try {
//            //获取cookie中的userId
//            Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);
//            //调用Service层修改密码方法
//            userService.updatePassword(userId,oldPassword,newPassword,repeatPassword);
//        } catch (ParamsException p) {
//            resultInfo.setCode(p.getCode());
//            resultInfo.setMsg(p.getMsg());
//            p.printStackTrace();
//        } catch (Exception e) {
//            resultInfo.setCode(500);
//            resultInfo.setMsg("修改密码失败!");
//            e.printStackTrace();
//        }
        return resultInfo;
    }

    @RequestMapping("toPasswordPage")
    public String toPasswordPage() {
        return  "user/password";
    }


    /*查询所有销售人员*/
    @RequestMapping("queryAllSales")
    @ResponseBody
    public List<Map<String,Object>> queryAllSales() {
        return userService.queryAllSales();
    }

    @RequestMapping("list")
    @ResponseBody
    public Map<String,Object> selectByParams(UserQuery userQuery) {
        return userService.queryByParamsForTable(userQuery);
    }

    @RequestMapping("index")
    public String index() {
        return "user/user";
    }

    @RequestMapping("add")
    @ResponseBody
    public ResultInfo addUser(User user) {
        userService.addUser(user);
        return success("用户添加成功!");
    }

    @RequestMapping("update")
    @ResponseBody
    public ResultInfo updateUser(User user) {
        userService.updateUser(user);
        return success("用户更新成功!");
    }

    /*打开添加或修改用户的页面*/
    @RequestMapping("toAddOrUpdateUserPage")
    public String toAddOrUpdateUserPage(Integer id,HttpServletRequest request) {
        //判断id是否为空，不为空表示更新操作
        if (id != null) {
            //通过id查询用户对象
            User user = userService.selectByPrimaryKey(id);
            //将数据设置到请求域中
            request.setAttribute("userInfo",user);
        }
        return "user/add_update";
    }

    @RequestMapping("delete")
    @ResponseBody
    public ResultInfo deleteUser(Integer[] ids) {
        userService.deleteById(ids);
        return success("用户删除成功!");
    }
}
